package studentdemo.dao;

import studentdemo.modul.Course;
import studentdemo.util.DBUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CourseDaoImpl implements CourseDao {
    @Override
    public int insertCourse(Course course) {
        String sql = "INSERT INTO Dujx_Course11 (djx_Cno11, djx_Cname11, djx_Csemester11, djx_Chours11, djx_Cexam11, djx_Ccredit11) VALUES (?, ?, ?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, course.getCno());
            ps.setString(2, course.getCname());
            ps.setString(3, course.getSemester());
            ps.setInt(4, course.getHours());
            ps.setString(5, course.getExam());
            ps.setInt(6, course.getCredit());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateCourse(Course course) {
        String sql = "UPDATE Dujx_Course11 SET djx_Cname11=?, djx_Csemester11=?, djx_Chours11=?, djx_Cexam11=?, djx_Ccredit11=? WHERE djx_Cno11=?";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, course.getCname());
            ps.setString(2, course.getSemester());
            ps.setInt(3, course.getHours());
            ps.setString(4, course.getExam());
            ps.setInt(5, course.getCredit());
            ps.setString(6, course.getCno());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int deleteCourse(String cno) {
        String sql = "DELETE FROM Dujx_Course11 WHERE djx_Cno11=?";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, cno);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Course getCourseById(String cno) {
        String sql = "SELECT * FROM Dujx_Course11 WHERE djx_Cno11=?";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, cno);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                return extractCourse(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Course> getCoursesByName(String name) {
        String sql = "SELECT * FROM Dujx_Course11 WHERE djx_Cname11 LIKE ?";
        List<Course> list = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, "%" + name + "%");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                list.add(extractCourse(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Course> getAllCourses(int page, int pageSize) {
        String sql = "SELECT * FROM Dujx_Course11 ORDER BY djx_Cno11 LIMIT ? OFFSET ?";
        List<Course> list = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, pageSize);
            ps.setInt(2, (page - 1) * pageSize);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                list.add(extractCourse(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int getCourseCount() {
        String sql = "SELECT COUNT(*) FROM Dujx_Course11";
        try (Connection conn = DBUtil.getConnection(); Statement st = conn.createStatement()) {
            ResultSet rs = st.executeQuery(sql);
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    private Course extractCourse(ResultSet rs) throws SQLException {
        Course c = new Course();
        c.setCno(rs.getString("djx_Cno11"));
        c.setCname(rs.getString("djx_Cname11"));
        c.setSemester(rs.getString("djx_Csemester11"));
        c.setHours(rs.getInt("djx_Chours11"));
        c.setExam(rs.getString("djx_Cexam11"));
        c.setCredit(rs.getInt("djx_Ccredit11"));
        return c;
    }
} 